﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace RemoteDesctopManager
{
    public partial class FormParent : Form
    {
        public FormParent()
        {
            InitializeComponent();
        }

        RDP_Params[] rdp;

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            CreateNewRDP((listBox1.SelectedItem as RDP_Params).IPadds, "администратор", "lj[kfzvsim", listBox1.SelectedItem.ToString(), (listBox1.SelectedItem as RDP_Params).Port.ToString());
        }

        private void FormParent_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("Каскад");
            comboBox1.Items.Add("Горизонтально");
            comboBox1.Items.Add("Вертикально");
            comboBox1.Items.Add("Свернуть все");

            GetListPharm();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CreateNewRDP(textBox1.Text, textBox2.Text, textBox3.Text, textBox1.Text, textBox4.Text);
        }

        private void CreateNewRDP(string serv,string usName,string pass,string nameForm,string port)
        {
            try
            {
                int port_i;
                if (port == "") port_i = 0;
                else port_i = Convert.ToInt32(port);

                int w = this.ClientSize.Width - listBox1.Width;
                int h = this.ClientSize.Height - panel1.Height - tabControl1.Height;
                FormChild fc = new FormChild()
                {
                    Server = serv,
                    UserName = usName,
                    Password = pass,
                    DesktopWidth = w,
                    DesktopHeight = h,
                    NameForm = nameForm,
                    Port = port_i
                };
                fc.MdiParent = this;
                fc.Show();
                fc.TabCtrl = tabControl1;
                TabPage tp = new TabPage();
                tp.Parent = tabControl1;
                tp.Text = fc.Text;
                tp.Show();
                fc.TabPag = tp;
                fc.Show();
                tabControl1.SelectedTab = tp;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Tab)
                textBox4.Focus();
            else
                if (!(Char.IsDigit(e.KeyChar)) && !(e.KeyChar == '.') && !(Char.IsLetter(e.KeyChar)))
                {
                    if (e.KeyChar != (char)Keys.Back)
                    {
                        e.Handled = true;
                    }
                }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Tab)
                textBox3.Focus();
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
                CreateNewRDP(textBox1.Text, textBox2.Text, textBox3.Text, textBox1.Text, textBox4.Text);
            if (e.KeyChar == (char)Keys.Tab)
                button1.Focus();
        }

        private void textBox2_ChangeUICues(object sender, UICuesEventArgs e)
        {
            textBox3.Clear();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((sender as ComboBox).Text == "Каскад")
            {
                foreach (Form f in this.MdiChildren)
                {
                    f.WindowState = FormWindowState.Normal;
                }
                this.LayoutMdi(MdiLayout.Cascade);
            }
            if ((sender as ComboBox).Text == "Горизонтально")
            {
                foreach (Form f in this.MdiChildren)
                {
                    f.WindowState = FormWindowState.Normal;
                }
                this.LayoutMdi(MdiLayout.TileHorizontal);
            }
            if ((sender as ComboBox).Text == "Вертикально")
            {
                foreach (Form f in this.MdiChildren)
                {
                    f.WindowState = FormWindowState.Normal;
                }
                this.LayoutMdi(MdiLayout.TileVertical);
            }
            if ((sender as ComboBox).Text == "Свернуть все")
            {
                foreach (Form f in this.MdiChildren)
                {
                    f.WindowState = FormWindowState.Minimized;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            GetListPharm();
        }

        private void GetListPharm()
        {
            try
            {
                SqlConnection conn = new SqlConnection(@"Server=sdata2;Database=Competitors;Integrated Security=True");
                conn.Open();
                SqlCommand comm = conn.CreateCommand();
                comm.CommandText = "sd2_PharmMonitor";
                comm.CommandType = CommandType.StoredProcedure;

                DataTable dt = new DataTable();
                dt.Load(comm.ExecuteReader());
                listBox1.Items.Clear();
                if (dt.Rows.Count > 0)
                {
                    rdp = new RDP_Params[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        int port = 0;
                        if(dt.Rows[i][11] != DBNull.Value) port = Convert.ToInt32(dt.Rows[i][11]);
                        rdp[i] = new RDP_Params()
                        {
                            Comment = dt.Rows[i][2].ToString(),
                            ClientID = Convert.ToInt32(dt.Rows[i][0]),
                            IPadds = dt.Rows[i][1].ToString(),
                            PharmCode = Convert.ToInt32(dt.Rows[i][4]),
                            Port = port
                        };
                        listBox1.Items.Add(rdp[i]);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void listBox1_Click(object sender, EventArgs e)
        {
            string ip = (listBox1.SelectedItem as RDP_Params).IPadds;
            int port = (listBox1.SelectedItem as RDP_Params).Port;
            if (port > 0)
            {
                textBox1.Text = ip;
                textBox4.Text = port.ToString();
            }
            else
            {
                textBox1.Text = ip;
                textBox4.Clear();
            }
            textBox2.Clear();
            textBox3.Clear();
            
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (FormChild childForm in this.MdiChildren)
            {
                if (childForm.TabPag.Equals(tabControl1.SelectedTab))
                {
                    childForm.Select();
                }
            }
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsDigit(e.KeyChar)))
            {
                if (e.KeyChar != (char)Keys.Back)
                {
                    e.Handled = true;
                }
            }
            if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Tab)
                textBox2.Focus();
        }
    }

    public partial class RDP_Params
    {
        public string Comment { get; set; }
        public int ClientID { get; set; }
        public string IPadds { get; set; }
        public int PharmCode { get; set; }
        public int Port { get; set; }

        public override string ToString()
        {
            string port = "";
            if (this.Port != 0) port = ":" + this.Port.ToString();
            return this.Comment + " (" + this.IPadds + port + ")";
        }
    }

    
}
